package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.YxStoreProductCheckmake;
import co.yixiang.modules.shop.repository.YxStoreProductCheckmakeRepository;
import co.yixiang.modules.shop.service.YxStoreProductCheckmakeService;
import co.yixiang.modules.shop.service.dto.YxStoreProductCheckmakeDto;
import co.yixiang.modules.shop.service.dto.YxStoreProductCheckmakeQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxStoreProductCheckmakeMapper;
import co.yixiang.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author frq
 * @date 2020-07-17
 */
@Service
//@CacheConfig(cacheNames = "yxStoreProductCheckmake")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreProductCheckmakeServiceImpl implements YxStoreProductCheckmakeService {

    private final YxStoreProductCheckmakeRepository yxStoreProductCheckmakeRepository;

    private final YxStoreProductCheckmakeMapper yxStoreProductCheckmakeMapper;

    public YxStoreProductCheckmakeServiceImpl(YxStoreProductCheckmakeRepository yxStoreProductCheckmakeRepository, YxStoreProductCheckmakeMapper yxStoreProductCheckmakeMapper) {
        this.yxStoreProductCheckmakeRepository = yxStoreProductCheckmakeRepository;
        this.yxStoreProductCheckmakeMapper = yxStoreProductCheckmakeMapper;
    }

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxStoreProductCheckmakeQueryCriteria criteria, Pageable pageable) {
        Page<Map> page = yxStoreProductCheckmakeRepository.findCheckmakeAll(criteria.getBarCode(), criteria.getProductName(), criteria.getBeginTime(), criteria.getEndTime(), pageable);
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getContent());
        map.put("totalElements", page.getTotalElements());
        return map;
    }

    @Override
    //@Cacheable
    public List<YxStoreProductCheckmakeDto> queryAll(YxStoreProductCheckmakeQueryCriteria criteria) {
        return yxStoreProductCheckmakeMapper.toDto(yxStoreProductCheckmakeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxStoreProductCheckmakeDto findById(Integer id) {
        YxStoreProductCheckmake yxStoreProductCheckmake = yxStoreProductCheckmakeRepository.findById(id).orElseGet(YxStoreProductCheckmake::new);
        ValidationUtil.isNull(yxStoreProductCheckmake.getId(), "YxStoreProductCheckmake", "id", id);
        return yxStoreProductCheckmakeMapper.toDto(yxStoreProductCheckmake);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxStoreProductCheckmakeDto create(YxStoreProductCheckmake resources) {
        resources.setAddTime(new Date());
        resources.setAdder(SecurityUtils.getUsername());
        return yxStoreProductCheckmakeMapper.toDto(yxStoreProductCheckmakeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreProductCheckmake resources) {
        YxStoreProductCheckmake yxStoreProductCheckmake = yxStoreProductCheckmakeRepository.findById(resources.getId()).orElseGet(YxStoreProductCheckmake::new);
        ValidationUtil.isNull(yxStoreProductCheckmake.getId(), "YxStoreProductCheckmake", "id", resources.getId());
        yxStoreProductCheckmake.copy(resources);
        yxStoreProductCheckmakeRepository.save(yxStoreProductCheckmake);
    }

    @Override
    public void deleteAll(Integer id) {
        yxStoreProductCheckmakeRepository.deleteById(id);
    }


}